<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">app</a>
        <a href="#">发现</a>
        <a href="#">运动游戏</a>
        <a href="#">添加游动游戏</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:window.location.reload();" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div style="width: 500px;padding-top: 10px">
    <form class="layui-form" action="" name="" onsubmit="return false" id="add">
        <input type="hidden" name="images" value="" lay-verify="images">
        <input type="hidden" name="video" value="" lay-verify="video">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" placeholder="标题" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="contents" placeholder="请输入内容" lay-verify="contents" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">目标</label>
            <div class="layui-input-block">
                <textarea name="target" placeholder="请输入内容" lay-verify="target" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">适合年龄</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="number" name="min" autocomplete="off" lay-verify="min" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="number" name="max"  autocomplete="off" lay-verify="max" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="upload_image">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item" id="images" style="display: none">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <img src="" width="150" height="150" id="new_upload_image">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">视频</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="upload_video">
                    <i class="layui-icon">&#xe67c;</i>上传视频
                </button>
                <span id="video_url" style="display: none"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                <button onclick="window.history.go(-1);" class="layui-btn layui-btn-primary">返回</button>
            </div>
        </div>
    </form>
</div>
</body>
<script>
    layui.use(['upload','form'], function(){
        var upload = layui.upload;
        var form = layui.form;
        //执行实例
        upload.render({
            elem: '#upload_image' //绑定元素
            ,url: '/sports_game/upload_image' //上传接口
            ,done: function(res){
                if(res.code == 200){
                    layer.msg('上传成功',{icon:1,time:2000});
                    $("#new_upload_image").attr('src',res.msg);
                    $("#images").css('display','block');
                    $("input[name=images]").val(res.msg);
                }else{
                    layer.msg('上传失败',{icon:5,time:2000});
                }
            }
        });
        upload.render({
            elem: '#upload_video' //绑定元素
            ,url: '/sports_game/upload_video' //上传接口
            ,accept:"video"
            ,before: function(obj){
                index = layer.msg('上传中请稍后', {
                    icon: 16
                    ,shade: 0.01
                    ,time:1000000
                });
            }
            ,done: function(res){
                if(res.code == 0){
                    layer.msg('上传成功',{icon:1,time:2000},function () {
                        layer.closeAll();
                    });
                    $("#video_url").html(res.video);
                    $("#video_url").css('display','block');
                    $("input[name=video]").val(res.video);
                    //http://tijian-sys.oss-cn-beijing.aliyuncs.com/b56b7ed3063239109a9d819325d8e5ac-C%3A%5CWindows%5Cphp8FF1.tmp.mp4
                }else{
                    layer.msg('上传失败',{icon:5,time:2000});
                }
            }
        });
        form.verify({
            title: function(value){
                if(!value){
                    return '请输入标题';
                }
            },
            contents:function (value) {
                if(!value){
                    return '请输入内容';
                }
            },
            min:function (value) {
                if(!value){
                    return '请输入年龄区间';
                }
            },
            max:function (value) {
                if(!value){
                    return '请输入年龄区间';
                }
            },
            images:function (value) {
                if(!value){
                    return '请上传图片';
                }
            },
            video:function (value) {
                if(!value){
                    return '请上传视频';
                }
            },
            target:function (value) {
                if(!value){
                    return '目标不能为空';
                }
            }
        });
        form.on('submit(*)', function(data) {
            $.ajax({
                type:'POST',
                url:'/sports_game/insert',
                data:$("#add").serialize(),
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000},function () {
                            window.location.reload();
                        });
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                    }
                }
            });
        });
    });
</script>
</html>